home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / wasm223.zip / FILE.ASM < prev    next >
Assembly Source File  |  1993-05-04  |  5KB  |  186 lines

  1. ;***************************************;
  2. ; WASM File Module                      ;
  3. ; By Eric Tauck                         ;
  4. ;                                       ;
  5. ; Defines:                              ;
  6. ;                                       ;
  7. ;   FilOpn  open an existing file       ;
  8. ;   FilCre  create or truncate a file   ;
  9. ;   FilClo  close an open file          ;
  10. ;   FilRea  read from a file            ;
  11. ;   FilWri  write to a file             ;
  12. ;   FilSee  move the read/write pointer ;
  13. ;***************************************;
  14.  
  15.         jmps    _file_end
  16.  
  17. ;========================================
  18. ; Global constants
  19.  
  20. ;--- open types for FilOpn
  21.  
  22. OPEN_READ       EQU     0       ;open for reading
  23. OPEN_WRITE      EQU     1       ;open for writing
  24. OPEN_BOTH       EQU     2       ;open for reading and writing
  25.  
  26. ;--- attributes for FilCre, attributes may be combined (ORed together)
  27.  
  28. ATTR_NORMAL     EQU     00000000B       ;normal file (no attributes)
  29. ATTR_READONLY   EQU     00000001B       ;readonly file
  30. ATTR_HIDDEN     EQU     00000010B       ;hidden file
  31. ATTR_SYSTEM     EQU     00000100B       ;system file
  32.  
  33. ;--- seek types for FilSee
  34.  
  35. SEEK_FRONT      EQU     0       ;from front, unsigned offset
  36. SEEK_CURRENT    EQU     1       ;from current location, signed offset
  37. SEEK_END        EQU     2       ;from end, signed offset
  38.  
  39. ;========================================
  40. ; Open an existing file.
  41. ;
  42. ; In: AX= address of file name; CL= open
  43. ;     type.
  44. ;
  45. ; Out: AX= DOS error code or file handle;
  46. ;      CY= set if error.
  47.  
  48. FilOpn  PROC   NEAR
  49.         mov     dx, ax          ;address of file name
  50.         mov     ah, 3DH         ;open function
  51.         mov     al, cl          ;open type
  52.         int     21H             ;execute
  53.         ret
  54.         ENDP
  55.  
  56. ;========================================
  57. ; Create a new file or truncate an
  58. ; existing one.
  59. ;
  60. ; In: AX= address of file name; CL= file
  61. ;     attribute.
  62. ;
  63. ; Out: AX= DOS error code or file handle;
  64. ;      CY= set if error.
  65.  
  66. FilCre  PROC   NEAR
  67.         mov     dx, ax          ;address of file name
  68.         mov     ah, 03CH        ;create file
  69.         sub     ch, ch          ;zero high byte of attribute
  70.         int     21H             ;execute
  71.         ret
  72.         ENDP
  73.  
  74. ;========================================
  75. ; Close a file.
  76. ;
  77. ; In: BX= file handle.
  78. ;
  79. ; Out: AL= DOS error code; CY= set if
  80. ;      error.
  81.  
  82. FilClo  PROC  NEAR
  83.         mov     ah, 3EH                 ;close file function
  84.         int     21H                     ;execute
  85.         ret
  86.         ENDP
  87.  
  88. ;========================================
  89. ; Read from a file.
  90. ;
  91. ; In: BX= file handle; DX:AX= address of
  92. ;     buffer; CX= bytes.
  93. ;
  94. ; Out: CX= bytes read; AL= DOS error
  95. ;      code (0 if EOF); CY= set if bytes
  96. ;      not all read.
  97.  
  98. FilRea  PROC   NEAR
  99.         push    ds
  100.         mov     ds, dx          ;
  101.         mov     dx, ax          ;address of buffer in DS:DX
  102.         mov     ah, 3FH         ;read file function
  103.         int     21H             ;execute
  104.         jc      _flrea1         ;jump if error
  105.         xchg    cx, ax          ;swap target and actual bytes
  106.         cmp     cx, ax          ;check if too few transfered
  107.         jb      _flrea2         ;jump if so
  108.         pop     ds
  109.         clc
  110.         ret
  111.  
  112. ;--- error returned by DOS
  113.  
  114. _flrea1 sub     cx, cx          ;zero bytes transfered
  115.         pop     ds
  116.         stc
  117.         ret
  118.  
  119. ;--- not all bytes read, but no error
  120.  
  121. _flrea2 sub     al, al          ;zero error code
  122.         pop     ds
  123.         stc
  124.         ret
  125.         ENDP
  126.  
  127. ;========================================
  128. ; Write to a file.
  129. ;
  130. ; In: BX= file handle; DX:AX= address of
  131. ;     buffer; CX= bytes.
  132. ;
  133. ; Out: CX= bytes written; AL= DOS error
  134. ;      code (0 if disk full); CY= set if
  135. ;      bytes not all written.
  136.  
  137. FilWri  PROC   NEAR
  138.         push    ds
  139.         mov     ds, dx          ;
  140.         mov     dx, ax          ;address of buffer in DS:DX
  141.         mov     ah, 40H         ;write file function
  142.         int     21H             ;execute
  143.         jc      _flwri1         ;jump if error
  144.         xchg    cx, ax          ;swap target and actual bytes
  145.         cmp     cx, ax          ;check if too few transfered
  146.         jb      _flwri2         ;jump if so
  147.         pop     ds
  148.         clc
  149.         ret
  150.  
  151. ;--- error returned by DOS
  152.  
  153. _flwri1 sub     cx, cx          ;zero bytes transfered
  154.         pop     ds
  155.         stc
  156.         ret
  157.  
  158. ;--- not all bytes written, but no error
  159.  
  160. _flwri2 sub     al, al          ;zero error code
  161.         pop     ds
  162.         stc
  163.         ret
  164.         ENDP
  165.  
  166. ;========================================
  167. ; Move a file read/write pointer.
  168. ;
  169. ; In: BX= file handle; CL= type of seek;
  170. ;     DX:AX= seek value.
  171. ;
  172. ; Out: AL= DOS error code; CY= set if
  173. ;      error.
  174.  
  175. FilSee  PROC   NEAR
  176.         push    cx
  177.         mov     cx, dx                  ;
  178.         mov     dx, ax                  ;seek value
  179.         pop     ax                      ;restore seek type
  180.         mov     ah, 42H                 ;seek function
  181.         int     21H                     ;execute
  182.         ret
  183.         ENDP
  184.  
  185. _file_end
  186.